﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MLOCore
{
    class SongFileComparer : IEqualityComparer<SongFile>
    {
        Single matchValue;

        // wow.  Magic comparison class...
        public SongFileComparer(Single matchThreshold)
        {
            matchValue = Math.Max(0, Math.Min(1, matchThreshold));
        }

        #region IEqualityComparer<SongFile> Members

        public bool Equals(SongFile x, SongFile y)
        {
            if (matchValue == 0) return false;

            if (x == null && y == null) return true;
            if (x == null || y == null) return false;

            float total = 0;
            int count = 0;

            foreach (NameValueNode item in x.Nodes)
            {
                NameValueNode item2 = y.Nodes.SingleOrDefault<NameValueNode>(n => n.FileInfo == item.FileInfo);

                if (item2 != null)
                {
                    total += item.Value.Compare(item2.Value);
                }
                count++;
            }

            // Do Average to find if this matches above the threshold
            if (count > 0 && total / count > matchValue)
                return true;
            else
                return false;

        }

        public int GetHashCode(SongFile obj)
        {
            return obj.GetHashCode();
        }

        #endregion
    }
}
